/*
  This file is part of jTopaz.
  http://gcl.cis.udel.edu/projects/jtopaz
  Copyright (C) 2009 University of Delaware

  jTopaz is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  jTopaz is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/

package jTopaz;

import org.globus.ftp.GridFTPClient;
import org.globus.ftp.FTPClient;
import java.io.File;
import org.globus.ftp.FileRandomIO;
import java.io.RandomAccessFile;

/**
   Class that handles the actual execution of an upload.  Uses the GridFTPClient extendedPut() method.
*/
public class UploadThread extends Thread {

    private GridFTPClient client;
    private FTPClient AnonClient;
	
    private File localFile;
    private String remoteFileName;
	
    private RandomAccessFile localFileRA;
    private FileRandomIO source;
	
    private boolean complete;
	
    /**
       Creates a new UploadThread object.
       @param client the client that will be doing the actual transfer.
       @param localFile the file that will be uploaded
       @param remoteFileName the name of the destination file
    */
    public UploadThread(GridFTPClient client, File localFile, String remoteFileName) {
	
	try {
	    this.client = client;
	    this.localFile = localFile;
	    this.remoteFileName = remoteFileName;
		
	    localFileRA = new RandomAccessFile(this.localFile, "r");
	    source = new FileRandomIO(localFileRA);
	    complete = false;	
	}
		
	catch (Exception e) {
	    System.out.println(e);
	}
    }
    public UploadThread(FTPClient AnonClient, File localFile, String remoteFileName) {
	
	try {
	    this.AnonClient = AnonClient;
	    this.localFile = localFile;
	    this.remoteFileName = remoteFileName;
		
	    localFileRA = new RandomAccessFile(this.localFile, "r");
	    source = new FileRandomIO(localFileRA);
	    complete = false;	
	}
		
	catch (Exception e) {
	    System.out.println(e);
	}
    }
	
    /**
       The actual execution of the upload
    */
    public void run() {
	
	try {
	    if(client!=null){
		//if the file already exists, delete it
		client.setPassive();
		client.setLocalActive();
		if (client.exists(remoteFileName)) {
		    System.out.println("deleting the file");
		    client.deleteFile(remoteFileName);				
		}
		
		client.setPassive();
		client.setLocalActive();
		client.extendedPut(remoteFileName, source, null);
		//client.put(localFile, remoteFileName, false);
		complete = true;
	    }
	    else {
		//if the file already exists, delete it
		AnonClient.setPassive();
		AnonClient.setLocalActive();
		if (AnonClient.exists(remoteFileName)) {
		    System.out.println("deleting the file");
		    AnonClient.deleteFile(remoteFileName);				
		}
		
		AnonClient.setPassive();
		AnonClient.setLocalActive();
		AnonClient.put(remoteFileName, source, null);
		//AnonClient.put(localFile, remoteFileName, false);
		complete = true;	
	    }
	}
		
	catch (Exception e) {
	    System.out.println(e);
	    e.printStackTrace();
	}	
    }
	
    /**
       Returns whether or not that upload has completed.
    */
    public boolean transferComplete() {
	return complete;	
    }
	
    public void cancel() {
    	this.interrupt();
    	complete = true;	
    }
}